package com.ruoyi.app.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.app.annotation.AppSecurity;
import com.ruoyi.app.service.AppListService;
import com.ruoyi.app.service.AppService;
import com.ruoyi.app.service.ContactService;
import com.ruoyi.app.util.ReqMsg;
import com.ruoyi.app.util.RspMsg;
import com.ruoyi.bizsys.domain.CustomerInfo;
import com.ruoyi.bizsys.domain.ProductInfo;
import com.ruoyi.bizsys.domain.UserFeedback;
import com.ruoyi.bizsys.service.IUserFeedbackService;
import com.ruoyi.business.Vo.CustReduceRes;
import com.ruoyi.business.service.DoCalCustReduce;
import com.ruoyi.business.service.IBProductService;
import com.ruoyi.business.service.QuickRepayService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.enums.RspEnmu;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.Func;
import com.ruoyi.common.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * swagger app前端接口
 *
 * @author ruoyi
 */
@Api("app前端需要身份认证接口")
@RestController
@Slf4j
@RequestMapping("/app")
public class AppController extends BaseController {

    @Autowired
    private AppService appService;

    @Autowired
    private IBProductService bProductService;

    @Autowired
    private IUserFeedbackService userFeedbackService;

    @Autowired
    DoCalCustReduce doCalCustReduce;

    @Autowired
    QuickRepayService quickRepayService;

    @Autowired
    private AppListService appListService;

    @Autowired
    private ContactService contactService;

    @AppSecurity
    @ApiOperation("设置登录密码")
    @Log(title = "设置登录密码", businessType = BusinessType.UPDATE)
    @PostMapping("/password")
    public RspMsg setLoginPwd(@Validated @RequestBody ReqMsg reqMsg) {
        //参数转换
        JSONObject dataJson = new JSONObject();
        if (null != reqMsg.getData() && StringUtils.isNotBlank(reqMsg.getData().toString())) {
            dataJson = JSONObject.parseObject(reqMsg.getData().toString());
        }

        dataJson.put("userId", getUserId());

        //请求参数校验
        if (null == dataJson.get("userId") || StringUtils.isBlank(dataJson.get("userId").toString())
                || null == dataJson.get("password") || StringUtils.isBlank(dataJson.get("password").toString())
        ) {
            throw new BusinessException(RspEnmu.PARAM_CANNOT_NULL);
        }

        JSONObject dataRsp = appService.setPassword(dataJson);
        return RspMsg.RspMsgOk(dataRsp);
    }

    @AppSecurity
    @ApiOperation("设置登录密码")
    @Log(title = "设置登录密码", businessType = BusinessType.UPDATE)
    @PostMapping("/resetPassword")
    public RspMsg resetPassword(@Validated @RequestBody ReqMsg reqMsg) {
        //参数转换
        JSONObject dataJson = new JSONObject();
        if (null != reqMsg.getData() && StringUtils.isNotBlank(reqMsg.getData().toString())) {
            dataJson = JSONObject.parseObject(reqMsg.getData().toString());
        }

        dataJson.put("userId", getUserId());

        //请求参数校验
        if (null == dataJson.get("userId") || StringUtils.isBlank(dataJson.get("userId").toString())
                || null == dataJson.get("password") || StringUtils.isBlank(dataJson.get("password").toString())
                || null == dataJson.get("oldPassword") || StringUtils.isBlank(dataJson.get("oldPassword").toString())
        ) {
            throw new BusinessException(RspEnmu.PARAM_CANNOT_NULL);
        }

        JSONObject dataRsp = appService.resetPassword(dataJson);
        return RspMsg.RspMsgOk(dataRsp);
    }

    @AppSecurity
    @ApiOperation("保存通讯录")
    @Log(title = "保存通讯录", businessType = BusinessType.INSERT)
    @PostMapping("/userContacts")
    public RspMsg userContacts(@Validated @RequestBody ReqMsg reqMsg) {
        //参数转换
        JSONObject dataJson = new JSONObject();
        if (null != reqMsg.getData() && StringUtils.isNotBlank(reqMsg.getData().toString())) {
            dataJson = JSONObject.parseObject(reqMsg.getData().toString());
        }

        dataJson.put("userId", getUserId());

        //请求参数校验
        if (StringUtils.isBlank(dataJson.get("bookList").toString())
                || null == dataJson.get("userId") || StringUtils.isBlank(dataJson.get("userId").toString())
        ) {
            throw new BusinessException(RspEnmu.PARAM_CANNOT_NULL);
        }

        JSONObject dataRsp = contactService.userContacts(dataJson);
        return RspMsg.RspMsgOk(dataRsp);
    }

    @AppSecurity
    @ApiOperation("客户通讯录授权")
    @Log(title = "客户通讯录授权", businessType = BusinessType.INSERT)
    @PostMapping("/custContacts")
    public RspMsg custContacts(@Validated @RequestBody ReqMsg reqMsg) {

        //参数转换
        JSONObject dataJson = new JSONObject();
        if (null != reqMsg.getData() && StringUtils.isNotBlank(reqMsg.getData().toString())) {
            dataJson = JSONObject.parseObject(reqMsg.getData().toString());
        }
        //请求参数校验
        if (Func.hasEmpty(dataJson.get("applyNo"), dataJson.get("actionType"))
        ) {
            throw new BusinessException(RspEnmu.PARAM_CANNOT_NULL);
        }
        //防重
        appService.repeatSubmit(dataJson.getString("applyNo"));
        return RspMsg.RspMsgOk(contactService.custContactsLock(dataJson, getUserId(), getCustNo(),getAppVersion()));
    }

    @AppSecurity
    @ApiOperation("客户通话记录授权")
    @Log(title = "客户通话记录授权", businessType = BusinessType.INSERT)
    @PostMapping("/custPhoneLog")
    public RspMsg custPhoneLog(@Validated @RequestBody ReqMsg reqMsg) {
        //参数转换
        JSONObject dataJson = new JSONObject();
        if (null != reqMsg.getData() && StringUtils.isNotBlank(reqMsg.getData().toString())) {
            dataJson = JSONObject.parseObject(reqMsg.getData().toString());
        }

        dataJson.put("userId", getUserId());

        //请求参数校验
        if (null == dataJson.get("callList") || StringUtils.isBlank(dataJson.get("callList").toString())
                || null == dataJson.get("userId") || StringUtils.isBlank(dataJson.get("userId").toString())
                || null == dataJson.get("applyNo") || StringUtils.isBlank(dataJson.get("applyNo").toString())
        ) {
            throw new BusinessException(RspEnmu.PARAM_CANNOT_NULL);
        }

        JSONObject dataRsp = appService.callList(dataJson);
        return RspMsg.RspMsgOk(dataRsp);
    }

    @AppSecurity
    @ApiOperation("客户短信记录授权")
    @Log(title = "客户短信记录授权", businessType = BusinessType.INSERT)
    @PostMapping("/custSmsLog")
    public RspMsg custSmsLog(@Validated @RequestBody ReqMsg reqMsg) {
        //参数转换
        JSONObject dataJson = new JSONObject();
        if (null != reqMsg.getData() && StringUtils.isNotBlank(reqMsg.getData().toString())) {
            dataJson = JSONObject.parseObject(reqMsg.getData().toString());
        }

        dataJson.put("userId", getUserId());

        //请求参数校验
        if (null == dataJson.get("smsList") || StringUtils.isBlank(dataJson.get("smsList").toString())
                || null == dataJson.get("userId") || StringUtils.isBlank(dataJson.get("userId").toString())
                || null == dataJson.get("applyNo") || StringUtils.isBlank(dataJson.get("applyNo").toString())
        ) {
            throw new BusinessException(RspEnmu.PARAM_CANNOT_NULL);
        }

        JSONObject dataRsp = appService.smsList(dataJson);
        return RspMsg.RspMsgOk(dataRsp);
    }

    @AppSecurity
    @ApiOperation("客户App信息授权")
    @Log(title = "客户App信息授权", businessType = BusinessType.INSERT)
    @PostMapping("/custAppInfo")
    public RspMsg custAppInfo(@Validated @RequestBody ReqMsg reqMsg) {

        //参数转换
        JSONObject dataJson = new JSONObject();
        if (null != reqMsg.getData() && StringUtils.isNotBlank(reqMsg.getData().toString())) {
            dataJson = JSONObject.parseObject(reqMsg.getData().toString());
        }
        //请求参数校验 dataJson.get("applyNo"),
        if (Func.hasEmpty(dataJson.get("actionType"), dataJson.getString("applyNo"))
        ) {
            throw new BusinessException(RspEnmu.PARAM_CANNOT_NULL);
        }
        //防重
        appService.repeatSubmit(dataJson.getString("applyNo"));
        //操作成功后向标记表更新字段
        return RspMsg.RspMsgOk(appListService.appListLock(dataJson, getUserId(), getCustNo()));
    }

    @AppSecurity
    @ApiOperation("客户日历信息授权")
    @Log(title = "客户日历信息授权", businessType = BusinessType.INSERT)
    @PostMapping("/custCalendarInfo")
    public RspMsg custCalendarInfo(@Validated @RequestBody ReqMsg reqMsg) {
        //参数转换
        JSONObject dataJson = new JSONObject();
        if (Func.isNotEmpty(reqMsg.getData())) {
            dataJson = JSONObject.parseObject(reqMsg.getData().toString());
        }
        return RspMsg.RspMsgOk(appService.addCalendarEvent(dataJson));
    }

    @AppSecurity
    @ApiOperation("获取客户信息是否过有效期")
    @Log(title = "获取客户信息是否过有效期", businessType = BusinessType.INSERT)
    @PostMapping("/custInfoExpire")
    public RspMsg custInfoExpire(@Validated @RequestBody ReqMsg reqMsg) {
        //参数转换
        JSONObject dataJson = new JSONObject();
//        if (null != reqMsg.getData() && StringUtils.isNotBlank(reqMsg.getData().toString())) {
//            dataJson = JSONObject.parseObject(reqMsg.getData().toString());
//        }
//
//        dataJson.put("userId", getUserId());
//
//        //请求参数校验
//        if (null == dataJson.get("userId") || StringUtils.isBlank(dataJson.get("userId").toString())) {
//            throw new BusinessException(RspEnmu.PARAM_CANNOT_NULL);
//        }
        dataJson = JSONObject.parseObject(JSON.toJSONString(reqMsg.getData()));
        JSONObject dataRsp = appService.getCustInfoExpire(dataJson);
        return RspMsg.RspMsgOk(dataRsp);
    }

    @AppSecurity
    @ApiOperation("更新客户信息有效期")
    @Log(title = "更新客户信息有效期", businessType = BusinessType.UPDATE)
    @PostMapping("/updateExpireCustInfo")
    public RspMsg updateExpireCustInfo(@Validated @RequestBody ReqMsg reqMsg) {
        //参数转换
        JSONObject dataJson = new JSONObject();
        /*if (null != reqMsg.getData() && StringUtils.isNotBlank(reqMsg.getData().toString())) {
            dataJson = JSONObject.parseObject(reqMsg.getData().toString());
        }

        dataJson.put("userId", getUserId());

        //请求参数校验
        if (null == dataJson.get("userId") || StringUtils.isBlank(dataJson.get("userId").toString())
                || null == dataJson.get("fieldList") || StringUtils.isBlank(dataJson.get("fieldList").toString())
        ) {
            throw new BusinessException(RspEnmu.PARAM_CANNOT_NULL);
        }*/

        dataJson = JSONObject.parseObject(JSON.toJSONString(reqMsg.getData()));
        JSONObject dataRsp = appService.updateExpireCustInfo(dataJson);
        return RspMsg.RspMsgOk(dataRsp);
    }

    @AppSecurity
    @ApiOperation("授权信息是否有效（7天内）")
    @Log(title = "授权信息是否有效（7天内）", businessType = BusinessType.GET)
    @PostMapping("/applyInfoExpire")
    public RspMsg applyInfoExpire(@RequestBody ReqMsg reqMsg) {
        JSONObject dataJson = new JSONObject();
        if (Func.isNotEmpty(reqMsg.getData())) {
            dataJson = JSONObject.parseObject(reqMsg.getData().toString());
        }
        //请求参数校验
        if (Func.isEmpty(dataJson.get("fingerprint"))) {
            throw new BusinessException(RspEnmu.PARAM_CANNOT_NULL);
        }
        dataJson.put("result", appService.applyInfoExpire(dataJson.getString("fingerprint")) == 0);
        return RspMsg.ok(dataJson);
    }

    @AppSecurity
    @ApiOperation("授权信息是否有效（7天内）V2")
    @Log(title = "授权信息是否有效（7天内）V2", businessType = BusinessType.GET)
    @PostMapping("/v2//applyInfoExpire")
    public RspMsg applyInfoExpireV2(@RequestBody ReqMsg reqMsg) {

        JSONObject dataJson = new JSONObject();
        if (Func.isNotEmpty(reqMsg.getData())) {
            dataJson = JSONObject.parseObject(reqMsg.getData().toString());
        }
        //请求参数校验
        if (Func.isEmpty(dataJson.get("fingerprint"))) {
            throw new BusinessException(RspEnmu.PARAM_CANNOT_NULL);
        }
        dataJson.put("result", appService.applyInfoExpire(dataJson.getString("fingerprint")));
        return RspMsg.ok(dataJson);
    }

    @AppSecurity
    @ApiOperation("设置交易密码")
    @Log(title = "设置交易密码", businessType = BusinessType.UPDATE)
    @PostMapping("/setTranPassword")
    public RspMsg setTranPassword(@Validated @RequestBody ReqMsg reqMsg) {
        //参数转换
        JSONObject dataJson = new JSONObject();
        if (null != reqMsg.getData() && StringUtils.isNotBlank(reqMsg.getData().toString())) {
            dataJson = JSONObject.parseObject(reqMsg.getData().toString());
        }

        dataJson.put("userId", getUserId());

        //请求参数校验
        if (null == dataJson.get("userId") || StringUtils.isBlank(dataJson.get("userId").toString())
                || null == dataJson.get("password") || StringUtils.isBlank(dataJson.get("password").toString())
        ) {
            throw new BusinessException(RspEnmu.PARAM_CANNOT_NULL);
        }

        JSONObject dataRsp = appService.setTransactionPassword(dataJson);
        return RspMsg.RspMsgOk(dataRsp);
    }

    @AppSecurity
    @ApiOperation("验证交易密码")
    @Log(title = "验证交易密码", businessType = BusinessType.AUTH)
    @PostMapping("/checkTranPassword")
    public RspMsg checkTranPassword(@Validated @RequestBody ReqMsg reqMsg) {
        //参数转换
        JSONObject dataJson = new JSONObject();
        if (null != reqMsg.getData() && StringUtils.isNotBlank(reqMsg.getData().toString())) {
            dataJson = JSONObject.parseObject(reqMsg.getData().toString());
        }

        dataJson.put("userId", getUserId());

        //请求参数校验
        if (null == dataJson.get("userId") || StringUtils.isBlank(dataJson.get("userId").toString())
                || null == dataJson.get("password") || StringUtils.isBlank(dataJson.get("password").toString())
        ) {
            throw new BusinessException(RspEnmu.PARAM_CANNOT_NULL);
        }

        JSONObject dataRsp = appService.checkTranPassword(dataJson);
        return RspMsg.RspMsgOk(dataRsp);
    }

    @AppSecurity
    @ApiOperation("获取回交易密码短信验证码")
    @Log(title = "获取回交易密码短信验证码", businessType = BusinessType.SMS)
    @PostMapping("/tranPasswordSmsCode")
    public RspMsg getTranPasswordSmsCode(@Validated @RequestBody ReqMsg reqMsg) {
        //参数转换
        JSONObject dataJson = new JSONObject();
        if (null != reqMsg.getData() && StringUtils.isNotBlank(reqMsg.getData().toString())) {
            dataJson = JSONObject.parseObject(reqMsg.getData().toString());
        }

        dataJson.put("userId", getUserId());

        //请求参数校验
        if (null == dataJson.get("userId") || StringUtils.isBlank(dataJson.get("userId").toString())
                || null == dataJson.get("bvnAccount") || StringUtils.isBlank(dataJson.get("bvnAccount").toString())
        ) {
            throw new BusinessException(RspEnmu.PARAM_CANNOT_NULL);
        }

        JSONObject dataRsp = appService.tranPasswordSmsCode(dataJson);
        return RspMsg.RspMsgOk(dataRsp);
    }

    @AppSecurity
    @ApiOperation("获取交易密码状态")
    @Log(title = "获取交易密码状态", businessType = BusinessType.GET)
    @PostMapping("/tranPasswordStatus")
    public RspMsg tranPasswordStatus(@Validated @RequestBody ReqMsg reqMsg) {
        //参数转换
        JSONObject dataJson = new JSONObject();
        if (null != reqMsg.getData() && StringUtils.isNotBlank(reqMsg.getData().toString())) {
            dataJson = JSONObject.parseObject(reqMsg.getData().toString());
        }

        dataJson.put("userId", getUserId());

        //请求参数校验
        if (null == dataJson.get("userId") || StringUtils.isBlank(dataJson.get("userId").toString())) {
            throw new BusinessException(RspEnmu.PARAM_CANNOT_NULL);
        }

        JSONObject dataRsp = appService.getTranPasswordStatus(dataJson);
        return RspMsg.RspMsgOk(dataRsp);
    }

    @AppSecurity
    @ApiOperation("找回交易密码")
    @Log(title = "找回交易密码", businessType = BusinessType.GET)
    @PostMapping("/forgetTranPassword")
    public RspMsg forgetTranPassword(@Validated @RequestBody ReqMsg reqMsg) {
        //参数转换
        JSONObject dataJson = new JSONObject();
        if (null != reqMsg.getData() && StringUtils.isNotBlank(reqMsg.getData().toString())) {
            dataJson = JSONObject.parseObject(reqMsg.getData().toString());
        }

        dataJson.put("userId", getUserId());

        //请求参数校验
        if (null == dataJson.get("userId") || StringUtils.isBlank(dataJson.get("userId").toString())
                || null == dataJson.get("bvnAccount") || StringUtils.isBlank(dataJson.get("bvnAccount").toString())
                || null == dataJson.get("verifyCode") || StringUtils.isBlank(dataJson.get("verifyCode").toString())
                || null == dataJson.get("uuid") || StringUtils.isBlank(dataJson.get("uuid").toString())
        ) {
            throw new BusinessException(RspEnmu.PARAM_CANNOT_NULL);
        }

        JSONObject dataRsp = appService.forgetTranPassword(dataJson);
        return RspMsg.RspMsgOk(dataRsp);
    }

    @AppSecurity
    @ApiOperation("获取贷款记录列表")
    @Log(title = "获取贷款记录列表", businessType = BusinessType.GET)
    @PostMapping("/getLoanList")
    public RspMsg getLoanList(@Validated @RequestBody ReqMsg reqMsg) {
        //参数转换
        JSONObject dataJson = new JSONObject();
        if (null != reqMsg.getData() && StringUtils.isNotBlank(reqMsg.getData().toString())) {
            dataJson = JSONObject.parseObject(reqMsg.getData().toString());
        }
        JSONObject dataRsp = appService.getLoanList(dataJson);
        return RspMsg.RspMsgOk(dataRsp);
    }

    @AppSecurity
    @ApiOperation("贷款记录详情查询")
    @Log(title = "贷款记录详情查询", businessType = BusinessType.GET)
    @PostMapping("/getLoanInfo")
    public RspMsg getLoanInfo(@Validated @RequestBody ReqMsg reqMsg) {
        //参数转换
        JSONObject dataJson = new JSONObject();
        if (null != reqMsg.getData() && StringUtils.isNotBlank(reqMsg.getData().toString())) {
            dataJson = JSONObject.parseObject(reqMsg.getData().toString());
        }

        dataJson.put("userId", getUserId());

        //请求参数校验
        if (null == dataJson.get("userId") || StringUtils.isBlank(dataJson.get("userId").toString())) {
            throw new BusinessException(RspEnmu.PARAM_CANNOT_NULL);
        }

        if (null == dataJson.get("loanNo") || StringUtils.isBlank(dataJson.get("loanNo").toString())) {
            throw new BusinessException(RspEnmu.PARAM_CANNOT_NULL);
        }

        JSONObject dataRsp = appService.getLoanInfo(dataJson);
        return RspMsg.RspMsgOk(dataRsp);
    }

    /**
     * 查询用户还款减免信息
     * http://confluence.lingxinhulian.cn/pages/viewpage.action?pageId=44204091
     *
     * @param reqMsg
     * @return
     */
    @AppSecurity
    @ApiOperation("显示是否减免")
    @Log(title = "显示是否减免", businessType = BusinessType.GET)
    @PostMapping("/getReduction")
    public RspMsg getReduction(@Validated @RequestBody ReqMsg reqMsg) {
        JSONObject dataJson = new JSONObject();
        if (Func.isNotEmpty(reqMsg.getData())) {
            dataJson = JSONObject.parseObject(reqMsg.getData().toString());
        }
        if (Func.hasEmpty(dataJson.get("loanSerialNo"))) {
            throw new BusinessException(RspEnmu.PARAM_CANNOT_NULL);
        }
        String loanSerialNo = (String) dataJson.get("loanSerialNo");
        CustReduceRes checkCustReduce = doCalCustReduce.checkCustReduce(loanSerialNo);
        JSONObject res = new JSONObject();
        res.put("qrepayinfo", checkCustReduce);
        res.put("monnifyinfo", null);
        return RspMsg.RspMsgOk(res);
    }


    @AppSecurity
    @ApiOperation("主动还款")
    @Log(title = "主动还款", businessType = BusinessType.SUBMIT)
    @PostMapping("/loanRepay")
    public RspMsg loanRepay(@Validated @RequestBody ReqMsg reqMsg) {
        //参数转换
        JSONObject dataJson = new JSONObject();
        if (null != reqMsg.getData() && StringUtils.isNotBlank(reqMsg.getData().toString())) {
            dataJson = JSONObject.parseObject(reqMsg.getData().toString());
        }

        dataJson.put("userId", getUserId());

        //请求参数校验
        if (null == dataJson.get("userId") || StringUtils.isBlank(dataJson.get("userId").toString())) {
            throw new BusinessException(RspEnmu.PARAM_CANNOT_NULL);
        }

        if (null == dataJson.get("loanNo") || StringUtils.isBlank(dataJson.get("loanNo").toString())) {
            throw new BusinessException(RspEnmu.PARAM_CANNOT_NULL);
        }

        if (null == dataJson.get("cardId") || StringUtils.isBlank(dataJson.get("cardId").toString())) {
            throw new BusinessException(RspEnmu.PARAM_CANNOT_NULL);
        }

        if (null == dataJson.get("bankCode") || StringUtils.isBlank(dataJson.get("bankCode").toString())) {
            throw new BusinessException(RspEnmu.PARAM_CANNOT_NULL);
        }

        if (null == dataJson.get("bankName") || StringUtils.isBlank(dataJson.get("bankName").toString())) {
            throw new BusinessException(RspEnmu.PARAM_CANNOT_NULL);
        }

        if (null == dataJson.get("password") || StringUtils.isBlank(dataJson.get("password").toString())) {
            throw new BusinessException(RspEnmu.PARAM_CANNOT_NULL);
        }
        // 获取是否使用了钱包
        if (null == dataJson.get("useWallet") || StringUtils.isBlank(dataJson.get("useWallet").toString())) {
            throw new BusinessException(RspEnmu.PARAM_CANNOT_NULL);
        }
        // 是否只用钱包
        if (null == dataJson.get("isJustWallet") || StringUtils.isBlank(dataJson.get("isJustWallet").toString())) {
            dataJson.put("isJustWallet",false);
        }
        //JSONObject dataRsp = appService.loanRepay(dataJson);
        JSONObject dataRsp = quickRepayService.loanRepay(dataJson);
        return RspMsg.RspMsgOk(dataRsp);
    }

    @AppSecurity
    @ApiOperation("主动还款-延期包")
    @Log(title = "主动还款-延期包", businessType = BusinessType.SUBMIT)
    @PostMapping("/loanRepayDerferred")
    public RspMsg loanRepayDerferred(@Validated @RequestBody ReqMsg reqMsg) {
        //参数转换
        JSONObject dataJson = new JSONObject();
        if (null != reqMsg.getData() && StringUtils.isNotBlank(reqMsg.getData().toString())) {
            dataJson = JSONObject.parseObject(reqMsg.getData().toString());
        }
        dataJson.put("userId", getUserId());
        //请求参数校验
        if (null == dataJson.get("userId") || StringUtils.isBlank(dataJson.get("userId").toString())) {
            throw new BusinessException(RspEnmu.PARAM_CANNOT_NULL);
        }
        if (null == dataJson.get("loanNo") || StringUtils.isBlank(dataJson.get("loanNo").toString())) {
            throw new BusinessException(RspEnmu.PARAM_CANNOT_NULL);
        }
        if (null == dataJson.get("cardId") || StringUtils.isBlank(dataJson.get("cardId").toString())) {
            throw new BusinessException(RspEnmu.PARAM_CANNOT_NULL);
        }
        if (null == dataJson.get("bankCode") || StringUtils.isBlank(dataJson.get("bankCode").toString())) {
            throw new BusinessException(RspEnmu.PARAM_CANNOT_NULL);
        }
        if (null == dataJson.get("bankName") || StringUtils.isBlank(dataJson.get("bankName").toString())) {
            throw new BusinessException(RspEnmu.PARAM_CANNOT_NULL);
        }
        if (null == dataJson.get("password") || StringUtils.isBlank(dataJson.get("password").toString())) {
            throw new BusinessException(RspEnmu.PARAM_CANNOT_NULL);
        }
        if (null == dataJson.get("deferredId") || StringUtils.isBlank(dataJson.get("deferredId").toString())) {
            throw new BusinessException(RspEnmu.PARAM_CANNOT_NULL);
        }
        // 获取是否使用了钱包
        if (null == dataJson.get("useWallet") || StringUtils.isBlank(dataJson.get("useWallet").toString())) {
            throw new BusinessException(RspEnmu.PARAM_CANNOT_NULL);
        }
        // 是否只用钱包
        if (null == dataJson.get("isJustWallet") || StringUtils.isBlank(dataJson.get("isJustWallet").toString())) {
            dataJson.put("isJustWallet",false);
        }
        // 检查延期包有效性
        boolean canBuy = quickRepayService.checkCanBuyDefer(dataJson.getInteger("deferredId"),dataJson.getString("loanNo"));
        if(!canBuy){
            throw new BusinessException(RspEnmu.PRODUCT_DEFERRED_IS_DOWN);
        }
        //JSONObject dataRsp = appService.loanRepay(dataJson);
        JSONObject dataRsp = quickRepayService.loanRepayDeferred(dataJson);
        return RspMsg.RspMsgOk(dataRsp);
    }

    @AppSecurity
    @ApiOperation("获取借据合同")
    @Log(title = "获取借据合同", businessType = BusinessType.GET)
    @PostMapping("/getLoanContract")
    public RspMsg getLoanContract(@Validated @RequestBody ReqMsg reqMsg) {
        //参数转换
        JSONObject dataJson = new JSONObject();
        if (null != reqMsg.getData() && StringUtils.isNotBlank(reqMsg.getData().toString())) {
            dataJson = JSONObject.parseObject(reqMsg.getData().toString());
        }

        dataJson.put("userId", getUserId());

        //请求参数校验
        if (null == dataJson.get("userId") || StringUtils.isBlank(dataJson.get("userId").toString())) {
            throw new BusinessException(RspEnmu.PARAM_CANNOT_NULL);
        }

        if (null == dataJson.get("loanNo") || StringUtils.isBlank(dataJson.get("loanNo").toString())) {
            throw new BusinessException(RspEnmu.PARAM_CANNOT_NULL);
        }

        JSONObject dataRsp = appService.getLoanContract(dataJson);
        return RspMsg.RspMsgOk(dataRsp);
    }

    @RequestMapping(value = "/contract/preview", method = RequestMethod.GET)
    @Log(title = "预览借据合同", businessType = BusinessType.GET)
    public void pdfStreamHandler(HttpServletRequest request, HttpServletResponse response) {
        String path = request.getParameter("path");

        path = "D:\\fd" + File.separator + path;

        //PDF文件地址
        File file = new File(path);
        if (file.exists()) {
            byte[] data = null;
            FileInputStream input = null;
            try {
                input = new FileInputStream(file);
                data = new byte[input.available()];
                input.read(data);
                response.getOutputStream().write(data);
            } catch (Exception e) {
                log.error("借据合同 {} 预览异常", path, e);
            } finally {
                try {
                    if (input != null) {
                        input.close();
                    }
                } catch (IOException e) {
                    log.error("借据合同预览异常", e);
                }
            }
        }
    }

    @AppSecurity
    @ApiOperation("获取默认产品")
    @Log(title = "获取默认产品", businessType = BusinessType.SUBMIT)
    @PostMapping("/product/default")
    public RspMsg getDefaultProductList(@Validated @RequestBody ReqMsg reqMsg) {

        CustomerInfo customerInfo = getCustomerInfo();
        //获取谷歌白名单验证账号
        String sysConfigValue = appService.getSysConfigValueByKey(appService.SYS_ACCOUNT_VERIFY_WHITELIST);
        String appVersion = getAppVersion();
        if (Integer.parseInt(appVersion.replaceAll("\\.", "")) < 205) {
            return RspMsg.RspMsgOk(bProductService.getActiveProductList(customerInfo.getCreditRating()));
        }

        if (this.appGpReviewSwitch()) {
            String appGpReviewVersion = getDictLabel("biz_switch", "app_gp_review_version");
            log.info("appVersion = {}, appGpReviewVersion = {}", appVersion, appGpReviewVersion);
            if ((formatAppVersion(appVersion) == formatAppVersion(appGpReviewVersion))
                    || (null != sysConfigValue && this.getUserOnline() && sysConfigValue.contains(this.getAppUser().getPhone()))) {
                ProductInfo productInfo = bProductService.getAppGpReviewProduct();
                List<ProductInfo> collect = new ArrayList<>();
                collect.add(productInfo);
                return RspMsg.RspMsgOk(collect);
            }
        }
        return RspMsg.RspMsgOk(bProductService.getActiveProductList(customerInfo.getCreditRating()));
    }

    @AppSecurity
    @ApiOperation("用户评价，新增差评弹窗")
    @Log(title = "用户评价，新增差评弹窗", businessType = BusinessType.INSERT)
    @PostMapping("/addUserFeed")
    public RspMsg addUserFeed(@Validated @RequestBody ReqMsg reqMsg) {
        // 获取登录者的随机id
        log.info("----------reqMsg:" + reqMsg);
        JSONObject dataJson = new JSONObject();
        // 参数转换
        if (null != reqMsg.getData() && StringUtils.isNotBlank(reqMsg.getData().toString())) {
            dataJson = JSONObject.parseObject(reqMsg.getData().toString());
        }
        UserFeedback userFeedback = new UserFeedback();
        // 对插入的数据赋值
        userFeedback.setUserId(getUserId());
        if (null != dataJson.getString("email") && StringUtils.isNotEmpty(dataJson.getString("email"))) {
            userFeedback.setEmail(dataJson.getString("email"));
        }
        if (null != dataJson.getString("content") && StringUtils.isNotEmpty(dataJson.getString("content"))) {
            userFeedback.setContent(dataJson.getString("content"));
        }

        if (null != dataJson.getString("operationType") && StringUtils.isNotEmpty(dataJson.getString("operationType"))) {
            userFeedback.setOperationType(dataJson.getString("operationType"));
        }
        if (null != dataJson.getString("score") && StringUtils.isNotEmpty(dataJson.getString("score"))) {
            userFeedback.setScore(Integer.valueOf(dataJson.getString("score")));
        }
        boolean save = userFeedbackService.save(userFeedback);
        return save ? RspMsg.RspMsgOk() : RspMsg.fail(RspEnmu.FEED_BACK_EXITS_FAIL);
    }


    @AppSecurity
    @ApiOperation("获取用户信用评级")
    @Log(title = "获取用户信用评级", businessType = BusinessType.GET)
    @PostMapping("/credit/rating")
    public RspMsg getCreditRating(@Validated @RequestBody ReqMsg reqMsg) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("creditRating",getCustomerInfo().getCreditRating());
        return RspMsg.RspMsgOk(jsonObject);
    }

    @AppSecurity
    @ApiOperation("获取延期金融包产品列表")
    @Log(title = "获取延期金融包产品列表", businessType = BusinessType.GET)
    @PostMapping("/getRepayList")
    public RspMsg getRepayList(@Validated @RequestBody ReqMsg reqMsg){
        JSONObject jsonObject = new JSONObject();
        if (null != reqMsg.getData() && StringUtils.isNotBlank(reqMsg.getData().toString())) {
            jsonObject = JSONObject.parseObject(reqMsg.getData().toString());
        }
        JSONObject dataRsp = appService.repayList(jsonObject);
        return RspMsg.RspMsgOk(dataRsp);
    }
}
